# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-depgraph
_pkgname=depgraph
pkgver=0.2.0
pkgrel=1
pkgdesc="A library and CLI tool for analyzing graph of dependencies between Lua modules"
url="https://github.com/mpeterv/depgraph"
arch="noarch"
license="MIT"
depends="lua lua-argparse lua-filesystem"
checkdepends="lua-busted"
source="$pkgname-$pkgver.tar.gz::https://github.com/mpeterv/$_pkgname/archive/$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	checkdepends="$checkdepends lua$_v lua$_v-busted"
done

check() {
	mkdir -p .bin

	local lver; for lver in $_luaversions; do
		msg "Testing on lua$lver"

		ln -sf /usr/bin/lua$lver .bin/lua
		LUA_PATH="$builddir/src/?.lua;$builddir/src/?/init.lua;;" \
			PATH="$builddir/.bin:$PATH" \
			lua$lver /usr/bin/busted
	done
}

package() {
	install -m 755 -D bin/luadepgraph.lua "$pkgdir"/usr/bin/luadepgraph
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver lua$lver-filesystem"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local lmod_dir="$subpkgdir/usr/share/lua/$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"

	cd "$builddir"
	mkdir -p "$lmod_dir"
	cp -r src/$_pkgname "$lmod_dir"/

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="1d399c7ba671fbae14a42696ca024a1c778db2df7dff81736f8d5e74fde547f2b74440ab2ab8f4e6c3ca60139ad7efc39a1c155608370b1c4572ce818edd4af5  lua-depgraph-0.2.0.tar.gz"
